package com.hkts.naturalgas.config;

import com.hkts.naturalgas.base.entity.*;
import com.hkts.naturalgas.base.service.*;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @ClassNameScheduleTask
 * @Description  用于定时同步工控系统数据
 * @Author yck
 * @Date2021-06-01 8:53
 * @Version V1.0
 **/
/*@Configuration      //1.主要用于标记配置类，兼备Component的效果。
*//*@EnableScheduling   // 2.开启定时任务
@Lazy(false)*/
public class ScheduleTask {
    @Autowired
    public ICalcinationDesulfurizationDenitrationService iCalcinationDesulfurizationDenitrationService;
    @Autowired
    public IRoastingPurifyWetElectricityService iRoastingPurifyWetElectricityService;
    @Autowired
    public IRoastingPurificationBleachingService iRoastingPurificationBleachingService;
    @Autowired
    public ICalcinationPurifyWetElectricityService iCalcinationPurifyWetElectricityService;
    @Autowired
    public ICalcinationPurificationBleachingService iCalcinationPurificationBleachingService;
    @Autowired
    public IRoastingDustService iRoastingDustService;
    @Autowired
    public IOzoneService iOzoneService;
    @Autowired
    public IRoastingDesulphurizingService iRoastingDesulphurizingService;
    @Autowired
    public IHktsLastTimeService iHktsLastTimeService;
    @Autowired
    public SqlServerProperties sqlServerProperties;
    /** 功能描述:
    *[]
    * @return: void
    * @Author: 杨春科
    * @Date: 2021-06-01 8:54
    * @Description //TODO
    */
   // @Scheduled(cron = "*/5 * * * * ?")
    private void configureTasks() throws Exception {
        Long startTime = System.currentTimeMillis();
        //上次最后一条记录时间
        HktsLastTime time=new HktsLastTime();
        time= iHktsLastTimeService.selectList(time).get(0);
        Connection connection = null;
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
        String dbURL = sqlServerProperties.getUrl();//数据源  ！！！注意若出现加载或者连接数据库失败一般是这里出现问题
        String Name = sqlServerProperties.getUsername();
        String Pwd = sqlServerProperties.getPassword();
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Class.forName(driverName);
        connection = DriverManager.getConnection(dbURL, Name, Pwd);
        String format = dtf2.format(time.getLastRowTime());
        PreparedStatement preparedStatement = connection.prepareStatement("select * from HKSJFW where CONVERT(datetime, 录入时间, 20) >'"+format+"' order by CONVERT(datetime, 录入时间, 20)" );
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println("---");
            String dateString = resultSet.getString("录入时间");
            Date parse = dateFormat.parse(dateString);
            LocalDateTime ldt = parse.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            time.setLastRowTime(ldt);
            // 焙烧除尘
            RoastingDust dust = new RoastingDust();
            String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());
            dust.setId(id);
            dust.setTime(ldt);
            dust.setBsjhAi1(resultSet.getFloat("BS_JH_CC_AI1_1"));
            dust.setBsjhAi2(resultSet.getFloat("BS_JH_CC_AI1_2"));
            dust.setBsjhHvc240067(resultSet.getFloat("BS_JH_CC_HVC240067"));
            dust.setBsjhHvc140068(resultSet.getFloat("BS_JH_CC_HVC140068"));
            dust.setBsjhHvc140067(resultSet.getFloat("BS_JH_CC_HVC140067"));
            dust.setBsjhHvc240068(resultSet.getFloat("BS_JH_CC_HVC240068"));
            dust.setBsjhHvc340067(resultSet.getFloat("BS_JH_CC_HVC340067"));
            dust.setBsjhHvc340068(resultSet.getFloat("BS_JH_CC_HVC340068"));
            dust.setBsjhHvc440067(resultSet.getFloat("BS_JH_CC_HVC440067"));
            dust.setBsjhHvc440068(resultSet.getFloat("BS_JH_CC_HVC440068"));
            dust.setBsjhHvc540067(resultSet.getFloat("BS_JH_CC_HVC540067"));
            dust.setBsjhHvc540068(resultSet.getFloat("BS_JH_CC_HVC540068"));
            dust.setBsjhHvc640067(resultSet.getFloat("BS_JH_CC_HVC640067"));
            dust.setBsjhHvc640068(resultSet.getFloat("BS_JH_CC_HVC640068"));
            iRoastingDustService.save(dust);
            //焙烧脱硫
            RoastingDesulphurizing roastingDesulphurizing = new RoastingDesulphurizing();
            roastingDesulphurizing.setId(id);
            roastingDesulphurizing.setTime(ldt);
            roastingDesulphurizing.setDataFc(resultSet.getFloat("Data_block_1_FC_OUT"));//
            roastingDesulphurizing.setDataLt(resultSet.getFloat("Data_block_1_LT1001"));
            roastingDesulphurizing.setDataNd(resultSet.getFloat("Data_block_1_ND1"));
            roastingDesulphurizing.setDataNox(resultSet.getFloat("Data_block_1_NXO_OUT"));
            roastingDesulphurizing.setDataFt(resultSet.getFloat("Data_block_1_FT_OUT"));
            roastingDesulphurizing.setDataO2(resultSet.getFloat("Data_block_1_O2_OUT"));
            roastingDesulphurizing.setDataPh(resultSet.getFloat("Data_block_1_PH1"));
            roastingDesulphurizing.setDataPt(resultSet.getFloat("Data_block_1_PT_OUT"));
            roastingDesulphurizing.setDataPt1003(resultSet.getFloat("Data_block_1_PT1003"));
            roastingDesulphurizing.setDataSo2(resultSet.getFloat("Data_block_1_SO2_OUT"));
            roastingDesulphurizing.setDataTe(resultSet.getFloat("Data_block_1_TE_OUT"));
            roastingDesulphurizing.setDataTe1002(resultSet.getFloat("Data_block_1_TE1002"));
            roastingDesulphurizing.setDataTe1003(resultSet.getFloat("Data_block_1_TE1003"));
            iRoastingDesulphurizingService.save(roastingDesulphurizing);
            // 焙烧净化湿电
            RoastingPurifyWetElectricity roastingPurifyWetElectricity = new RoastingPurifyWetElectricity();
            roastingPurifyWetElectricity.setId(id);
            roastingPurifyWetElectricity.setTime(ldt);
            roastingPurifyWetElectricity.setBsjhSd2i2(resultSet.getFloat("BS_JH_SD1_GY1_I2"));
            roastingPurifyWetElectricity.setBsjhSd2v2(resultSet.getFloat("BS_JH_SD1_GY1_V2"));
            roastingPurifyWetElectricity.setBsjhSdi2(resultSet.getFloat("BS_JH_SD2_GY2_I2"));
            roastingPurifyWetElectricity.setBsjhSdv2(resultSet.getFloat("BS_JH_SD2_GY2_V2"));
            iRoastingPurifyWetElectricityService.save(roastingPurifyWetElectricity);
            //焙烧净化脱白
            RoastingPurificationBleaching roastingPurificationBleaching = new RoastingPurificationBleaching();
            roastingPurificationBleaching.setId(id);
            roastingPurificationBleaching.setTime(ldt);
            roastingPurificationBleaching.setBsjhFan1dl(resultSet.getFloat("BS_JH_TB2_FAN_1_DL"));
            roastingPurificationBleaching.setBsjhFan1sv(resultSet.getFloat("BS_JH_TB2_FAN_1_SV"));
            roastingPurificationBleaching.setBsjhSidu(resultSet.getFloat("BS_JH_TB2_SIDU"));
            roastingPurificationBleaching.setBsjhFan2dl(resultSet.getFloat("BS_JH_TB2_FAN_2_DL"));
            roastingPurificationBleaching.setBsjhFan2sv(resultSet.getFloat("BS_JH_TB2_FAN_2_SV"));
            roastingPurificationBleaching.setBsjhSidu2(resultSet.getFloat("BS_JH_TB2_SIDU_2"));
            roastingPurificationBleaching.setBsjhTe102(resultSet.getFloat("BS_JH_TB2_TE102"));
            roastingPurificationBleaching.setBsjhTe105(resultSet.getFloat("BS_JH_TB2_TE105"));
            iRoastingPurificationBleachingService.save(roastingPurificationBleaching);
            //煅烧净化湿电
            CalcinationPurifyWetElectricity calcinationPurifyWetElectricity = new CalcinationPurifyWetElectricity();
            calcinationPurifyWetElectricity.setId(id);
            calcinationPurifyWetElectricity.setTime(ldt);
            calcinationPurifyWetElectricity.setDsjhSda1(resultSet.getFloat("DS_JH_SD2_一电场二次电流"));
            calcinationPurifyWetElectricity.setDsjhSda2(resultSet.getFloat("DS_JH_SD2_A二次电压"));
            calcinationPurifyWetElectricity.setDsjhSdb1(resultSet.getFloat("DS_JH_SD3_二电场二次电流"));
            calcinationPurifyWetElectricity.setDsjhSdb2(resultSet.getFloat("DS_JH_SD3_B二次电压"));
            iCalcinationPurifyWetElectricityService.save(calcinationPurifyWetElectricity);
            //煅烧净化脱白
            CalcinationPurificationBleaching calcinationPurificationBleaching=new CalcinationPurificationBleaching();
            calcinationPurificationBleaching.setDsjhFanadl(resultSet.getFloat("DS_JH_TB_FAN_A_DL"));
            calcinationPurificationBleaching.setDsjhFanasv(resultSet.getFloat("DS_JH_TB_FAN_A_SV"));
            calcinationPurificationBleaching.setDsjhFanbdl(resultSet.getFloat("DS_JH_TB_FAN_B_DL"));
            calcinationPurificationBleaching.setDsjhFanbsv(resultSet.getFloat("DS_JH_TB_FAN_B_SV"));
            calcinationPurificationBleaching.setDsjhFancdl(resultSet.getFloat("DS_JH_TB_FAN_C_DL"));
            calcinationPurificationBleaching.setDsjhFancsv(resultSet.getFloat("DS_JH_TB_FAN_C_SV"));
            calcinationPurificationBleaching.setDsjhSidu(resultSet.getFloat("DS_JH_TB_SIDU"));
            calcinationPurificationBleaching.setDsjhTe103(resultSet.getFloat("DS_JH_TB_TE103"));
            calcinationPurificationBleaching.setDsjhTe108(resultSet.getFloat("DS_JH_TB_TE108"));
            calcinationPurificationBleaching.setDsjhTe110(resultSet.getFloat("DS_JH_TB_TE110"));
            calcinationPurificationBleaching.setId(id);
            calcinationPurificationBleaching.setTime(ldt);
            iCalcinationPurificationBleachingService.save(calcinationPurificationBleaching);
            //煅烧脱硫脱硝
            CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration=new CalcinationDesulfurizationDenitration();
            // 目前系统没有的四个参数
            calcinationDesulfurizationDenitration.setDataFc(resultSet.getFloat("Data_block_1_FC_OUT"));
            calcinationDesulfurizationDenitration.setDataNox(resultSet.getFloat("Data_block_1_NXO_OUT"));
            calcinationDesulfurizationDenitration.setDataSo2(resultSet.getFloat("Data_block_1_SO2_OUT"));
            calcinationDesulfurizationDenitration.setDataO2(resultSet.getFloat("Data_block_1_O2_OUT"));

            calcinationDesulfurizationDenitration.setDsjhFt101(resultSet.getFloat("DS_JH_TX_FT101"));
            calcinationDesulfurizationDenitration.setDsjhFt102(resultSet.getFloat("DS_JH_TX_FT102"));
            calcinationDesulfurizationDenitration.setDsjhP111(resultSet.getFloat("DS_JH_TX_P111"));
            calcinationDesulfurizationDenitration.setDsjhP121(resultSet.getFloat("DS_JH_TX_P121"));
            calcinationDesulfurizationDenitration.setDsjhT111(resultSet.getFloat("DS_JH_TX_T111"));
            calcinationDesulfurizationDenitration.setDsjhTllt(resultSet.getFloat("DS_JH_TL_LT_101_1"));
            calcinationDesulfurizationDenitration.setDsjhT121(resultSet.getFloat("DS_JH_TX_T121"));
            calcinationDesulfurizationDenitration.setDsjhTlmd(resultSet.getFloat("DS_JH_TL_MD_102"));
            calcinationDesulfurizationDenitration.setDsjhTlph(resultSet.getFloat("DS_JH_TL_PH_101"));
            calcinationDesulfurizationDenitration.setId(id);
            calcinationDesulfurizationDenitration.setTime(ldt);
            iCalcinationDesulfurizationDenitrationService.save(calcinationDesulfurizationDenitration);
            //臭氧
            Ozone ozone=new Ozone();
            ozone.setBsjhCy1(resultSet.getFloat("BS_JH_CY1_出气流量"));
            ozone.setBsjhCy1c(resultSet.getFloat("BS_JH_CY1_臭氧浓度"));
            ozone.setBsjhCy2c(resultSet.getFloat("BS_JH_CY2_臭氧浓度"));
            ozone.setBsjhCy2f(resultSet.getFloat("BS_JH_CY2_出气流量"));
            ozone.setId(id);
            ozone.setTime(ldt);
            iOzoneService.save(ozone);
        }
        connection.close();
        iHktsLastTimeService.updateById(time);

        System.out.println("更新成功");
        Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - startTime);
        System.out.println("定时任务花费时间："+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
    }
}
